<?php

//Function to make a database connection
function makeConnection() {
    //Setup variable ***CHANGE HERE ONLY***
   
    $host='localhost'; //country 0
    $username='s91199'; //username 0
    $password='123456'; //password 0
    $dbname='s91199'; //name 0
    /*
    $host='localhost'; //country 0
    $username='root'; //username 0
    $password=''; //password 0
    $dbname='mydb'; //name 0*/
    try {
        $connection = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
        return $connection;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for escaping string
 * Return data: string (escaped)
*/

function escapeString($data) {
    try {
        $dbh = makeConnection();
        $escaped_data = $dbh->quote($data);
        return $escaped_data;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for SELECT statement
 * Return data: ASSOCIATIVE ARRAY for all rows returned
*/

function executeSelectQuery($query) {
    try {
        $dbh = makeConnection();
        $sth = $dbh->prepare($query);
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for DELETE,UPDATE,DELET statement
 * Return data: Number of rows affected
 * NOTE: Cannot use for SELECT statement
*/

function executeQuery($query) {
    try {
        $dbh = makeConnection();
        $count = $dbh->exec($query);
        return $count;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for COUNT TOTAL COLUMNS
 * Return data: int: Total Columns
*/

function countColumns($query) {
    try {
        $dbh = makeConnection();
        $sth = $dbh->prepare($query);
        $sth->execute();
        $count = $sth->columnCount();
        return $count;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function for COUNT TOTAL ROWS
 * Return data: int: Total Rows
*/
function countRows($query) {
    try {
        $dbh = makeConnection();
        $sth = $dbh->prepare($query);
        $sth->execute();
        $count = $sth->rowCount();
        return $count;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

/* Function export To JSON format to be used by HighCharts
 * Parameter: + $queryResult - Associative array contains query result
 *            + $columnName - name of column to display at X axis
 * Return data: JSON array {[xAxis data],[yAxis data]}
*/

function exportToJSONChart($queryResult, $columnName) {
    $xAxisArray = Array(); //Store x Axis name
    $yAxisArray = Array(); //Store data

    for ($i = 0; $i < count($queryResult); $i++) {
        $xAxisArray[] = $queryResult[$i][$columnName];
    }

    for ($i = 0; $i < count($queryResult); $i++) {
        $yAxisArray[] = (int) $queryResult[$i]['y'];
    }

    $dataJSON = Array();
    $dataJSON[0] = $xAxisArray;
    $dataJSON[1] = $yAxisArray;

    return json_encode($dataJSON);
}


?>
